home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / ViePratique / gnucash / gnucash-2.6.5-setup.exe / {app} / bin / intltool-prepare~ < prev    next >
Text File  |  2008-09-23  |  9KB  |  344 lines

  1. #!/opt/perl/bin/perl -w
  2. # -*- Mode: perl; indent-tabs-mode: nil; c-basic-offset: 4  -*-
  3.  
  4. #  Intltool .desktop, .directory Prepare Tool
  5. #
  6. #  Copyright (C) 2001 Free Software Foundation.
  7. #
  8. #  Intltool is free software; you can redistribute it and/or
  9. #  modify it under the terms of the GNU General Public License as
  10. #  published by the Free Software Foundation; either version 2 of the
  11. #  License, or (at your option) any later version.
  12. #
  13. #  Intltool is distributed in the hope that it will be useful,
  14. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. #  General Public License for more details.
  17. #
  18. #  You should have received a copy of the GNU General Public License
  19. #  along with this program; if not, write to the Free Software
  20. #  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. #
  22. #  Author(s): Gediminas Paulauskas <menesis@delfi.lt>
  23. #          Kenneth Christiansen <kenneth@gnu.org>
  24.  
  25. ## Release information
  26. my $PROGRAM      = "intltool-prepare";
  27. my $PACKAGE      = "intltool";
  28. my $VERSION      = "0.40.4";
  29.  
  30. ## Loaded modules
  31. use strict;
  32. use Getopt::Long;
  33. use File::Find;
  34.  
  35. ## Scalars used by the option stuff
  36. my $HELP_ARG    = "0";
  37. my $VERSION_ARG = "0";
  38. my $VERBOSE     = "0";
  39.  
  40. my @languages;
  41. my @desktop_files;
  42. my $new;
  43. my $file;
  44.  
  45. my $desktop_extension = "(desktop|soundlist|keys|directory)";
  46.  
  47. my $keywords = "Name|Comment|GenericName|SwallowTitle|description";
  48.  
  49. ## Always print as the first thing
  50. $| = 1;
  51.  
  52. ## Handle options
  53. GetOptions (
  54.             "help|h"            => \$HELP_ARG,
  55.             "version|v"         => \$VERSION_ARG,
  56.             "verbose|x"         => \$VERBOSE
  57.             ) or &invalid_option;
  58.  
  59.  
  60. ## Use the supplied arguments
  61. ## Check for options.
  62. ## This section will check for the different options.
  63.  
  64. sub split_on_argument {
  65.  
  66.     if ($VERSION_ARG) {
  67.         &version;
  68.  
  69.     } elsif ($HELP_ARG) {
  70.         &help;
  71.  
  72.     } else {
  73.         &main;
  74.     }
  75. }
  76.  
  77. &split_on_argument;
  78.  
  79. sub main
  80. {
  81.     print "Working, please wait...\n" if (! $VERBOSE);
  82.     &find_desktop_files;
  83.     &append_keywords;
  84.     &add_to_potfiles;
  85.     &perform_rescue;
  86.     &add_to_cvsignore;
  87.     &fix_makefiles;
  88.     &generate_empty;
  89. }
  90.  
  91. sub version {
  92.     print <<_EOF_;
  93. ${PROGRAM} ${PACKAGE} $VERSION
  94. Written by Gediminas Paulauskas <menesis\@delfi.lt>, 2000.
  95.  
  96. Copyright (C) 2000 Free Software Foundation, Inc.
  97. This is free software; see the source for copying conditions.  There is NO
  98. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  99. _EOF_
  100.     exit;
  101. }
  102.  
  103. sub help
  104. {
  105.     print <<_EOF_;
  106. Usage: ${PROGRAM} [OPTION] KEYWORD...
  107. Automates preparation steps before software make use of intltool.
  108. KEYWORD is a list of additional key other than "Name", "Comment"
  109. and "description".
  110.  
  111.   -h, --help                 shows this help page
  112.   -v, --version              shows the version
  113.   -x, --verbose              show lots of feedback
  114.  
  115. Report bugs to http://bugzilla.gnome.org/ (product name "$PACKAGE")
  116. or send email to <xml-i18n-tools\@gnome.org>.
  117. _EOF_
  118.     exit;
  119. }
  120.  
  121. sub invalid_option
  122. {
  123.     ## Handle invalid arguments
  124.     ## my $opt = $ARGV[0];
  125.     ## print "$PROGRAM: invalid option -- $opt\n";
  126.     print STDERR "Try `$PROGRAM --help' for more information.\n";
  127.     exit 1;
  128. }
  129.  
  130. sub append_keywords
  131. {
  132.     my $arg;
  133.     foreach $arg (@ARGV) {
  134.         $keywords .= "|$arg";
  135.     }
  136. }
  137.  
  138. sub add_to_potfiles
  139. {
  140.     open FILE, ">>po/POTFILES.in";
  141.     my $intro = 0;
  142.     foreach my $desktop (@desktop_files) {
  143.         next if contains("po/POTFILES.in", "$desktop.in");
  144.     # Print explanation comment only once
  145.         unless ($intro) {
  146.            print FILE "# files added by intltool-prepare\n";
  147.            $intro = 1;
  148.         }
  149.         print FILE "$desktop.in\n";
  150.     }
  151.     close FILE;
  152. }
  153.  
  154. sub perform_rescue
  155. {
  156.     foreach $file (@desktop_files) {
  157.         &rescue_file($file);
  158.     }
  159. }
  160.  
  161. sub rescue_file
  162. {
  163.     my ($filename) = @_;
  164.     my ($msgid, $line, $lang);
  165.  
  166.     print "Rescuing translations from $filename ...\n" if $VERBOSE;
  167.  
  168.     open ORIG, "<$filename";
  169.     $line = 1;
  170. ENTRY: while (<ORIG>) {
  171.         chomp;
  172.         $line++;
  173.         my $entry = $_;
  174.         if (($entry =~ /^($keywords)=(.*)$/) ||
  175.             ($entry =~ /^(\s*description)=(.*)$/)) {
  176.             $msgid = $2;
  177.             $msgid =~ s/\\/\\\\/g;
  178.         $msgid =~ s/\"/\\"/g;
  179.         } elsif (($entry =~ /^($keywords)\[(.*?)\]=(.*)$/) ||
  180.                  ($entry =~ /^(\s*\[)(.*?)\]description=(.*)$/)) {
  181.             $lang = $2;
  182.  
  183.             my $msgstr = $3;
  184.             $msgstr =~ s/\\/\\\\/g;
  185.             $msgstr =~ s/"/\\"/g;
  186.         
  187.             $line--;
  188.             if ((-s "po/$lang.po") && 
  189.                 (contains("po/$lang.po", "msgid \"$msgid\""))) {
  190.                 next ENTRY;
  191.             }
  192.             
  193.             open POFILE, ">>po/$lang.po";
  194.  
  195.             print POFILE "\n#: $filename.in:$line\n";
  196.             print POFILE "msgid \"$msgid\"\n";
  197.             print POFILE "msgstr \"$msgstr\"\n";
  198.  
  199.             close POFILE;
  200.         }
  201.     }
  202. }
  203.  
  204. sub generate_empty
  205. {
  206.     my $all = ' ';
  207.     foreach my $full (@desktop_files) {
  208.         $new = "$full.in";
  209.         $all .= "$new ";
  210.         print "Generating empty $new ...\n" if $VERBOSE;
  211.         open FULL, "<$full";
  212.         open NEW, ">$new";
  213.  
  214.         while (<FULL>) {
  215.             unless (
  216.                     (/^($keywords)\[.*?\]=.*$/) ||
  217.                     (/^\s*\[(.*?)\]description=.*$/)
  218.                    ) {
  219.                 if (/^($keywords)=.*$/) {
  220.                     print NEW "_$_";
  221.                 } elsif (/^(\s*)(description=.*)$/) {
  222.                     print NEW "$1_$2\n";
  223.                 } else {
  224.                     print NEW;
  225.                 }
  226.             }
  227.         }
  228.  
  229.         close NEW;
  230.     }
  231.     unless ($all eq ' ') {
  232.         print "*** Please add these files to CVS by following command: ***\n"
  233.             . "cvs add$all\n";
  234.     }
  235. }
  236.  
  237. sub add_to_cvsignore
  238. {
  239.     my $all = ' ';
  240.     my $ign;
  241.     foreach $file (@desktop_files) {
  242.         $file =~ /^(.*\/)?(.+?\.$desktop_extension$)$/;
  243.         if ($1) {
  244.             $ign = "$1.cvsignore";
  245.         } else {
  246.             $ign = ".cvsignore";
  247.         }
  248.         my $basename = $2;
  249.  
  250.         next if contains($ign, $basename);
  251.         
  252.         print "Appending $basename to $ign\n" if $VERBOSE;
  253.         open FILE, ">>$ign";
  254.         print FILE "$basename\n";
  255.         $all .= "$file ";
  256.     }
  257.     close FILE;
  258.     unless ($all eq ' ') {
  259.         print "*** Please remove files from CVS by following command: ***\n"
  260.             . "cvs remove -f$all\n";
  261.     }
  262. }
  263.  
  264. sub fix_makefiles
  265. {
  266.     my $file;
  267.     foreach $file (@desktop_files) {
  268.         my ($makefile, $line);
  269.  
  270.         $file =~ /^(.*\/)?(.+?\.$desktop_extension$)$/;
  271.         if ($1) {
  272.             $makefile = "$1Makefile.am";
  273.         } else {
  274.             $makefile = "Makefile.am";
  275.         }
  276.         my $basename = $2;
  277.         print "Fixing $basename entry in $makefile\n" if $VERBOSE;
  278.  
  279.         open MAKE, $makefile;
  280.         open NEWMAKE, ">$makefile.new";
  281.         my $extra = 0;
  282.         while ($line = <MAKE>) {
  283.             $extra = 1 if $line =~ /^EXTRA_DIST/;
  284.             if ($extra) {
  285.                 if (($line =~ /$basename/) &&
  286.                     !($line =~ /$basename\.in/)) {
  287.                     $line =~ s/$basename/$basename\.in/;
  288.                 }
  289.                 $extra = 0 unless $line =~ /\\\s*$/
  290.             } else {
  291.                 if ($line =~ /^(\w+)_DATA\s*=\s*$basename\s*$/) {
  292.                     my $name = $1;
  293.                     $line =~ s/^$name\_DATA/$name\_in_files/;
  294.                     $line =~ s/$basename/$basename\.in/;
  295.                     $basename =~ /\.($desktop_extension)$/;
  296.                     my $ext = $1;
  297.                     $line .= "$name\_DATA = \$($name\_in_files:.$ext.in=.$ext)\n";
  298.                     $ext =~ tr/a-z/A-Z/;
  299.                     if (!contains($makefile, "\@INTLTOOL_$ext\_RULE\@")) {
  300.                         $line .= "\@INTLTOOL_$ext\_RULE\@\n";
  301.                     }
  302.                 }
  303.             }
  304.             print NEWMAKE $line;
  305.         }
  306.         close NEWMAKE;
  307.         rename "$makefile.new", $makefile;
  308.     }
  309. }
  310.  
  311. sub contains
  312. {
  313.     my ($name, $str) = @_;
  314.     open CONT, "<$name";
  315.     while (<CONT>) {
  316.          chomp;
  317.          return 1 if $_ eq $str;
  318.     }
  319.     return 0;
  320. }
  321.  
  322. sub find_desktop_files
  323. {
  324.     if ($VERBOSE) {
  325.         print "Found these interesting files:\n";
  326.     } else {
  327.         print "Finding interesting files...";
  328.     }
  329.     find (\&wanted, '.');
  330.     print "done\n" unless $VERBOSE;
  331. }
  332.  
  333. sub wanted
  334. {
  335.     if (/\.$desktop_extension$/) {
  336.         my $file = $File::Find::name;
  337.         $file =~ s/\.\///;
  338.         push @desktop_files, $file;
  339.         print "$file\n" if $VERBOSE;
  340.     }
  341. }
  342.  
  343. # vim: ts=4 sw=4 expandtab
  344.